<?php

App::Import('Vendor', 'infofile');

class InfofileTestCase extends CakeTestCase {

    function setUp() {
        $filename = tempnam(null, 'test_');
        $temp = fopen($filename, "w");
        fwrite($temp, "Name: 测试数据\nLeaf: False\nDescription::\n test\n test again\n\n");
        fwrite($temp, "File: abc.zip\nName: ABC\nDownloadCount: 10000\n");
        fclose($temp);

        $this->filename = $filename;
    }

    function tearDown() {
        unlink($this->filename);
    }

    function testInfoFileRead() {
        $fileinfo = new InfoFile($this->filename);
        $info = $fileinfo->load();
        $this->assertTrue(array_key_exists('__DIR__', $info));
        $this->assertEqual('测试数据', $info['__DIR__']['Name']);
        $this->assertEqual('False', $info['__DIR__']['Leaf']);
        $this->assertEqual("test\ntest again\n", $info['__DIR__']['Description']);
        
        $this->assertTrue(array_key_exists('abc.zip', $info));
        $this->assertEqual('ABC', $info['abc.zip']['Name']);
        $this->assertEqual('10000', $info['abc.zip']['DownloadCount']);
    }

    function testInfoFileWrite() {
        $fileinfo = new InfoFile($this->filename);
        $info = $fileinfo->load();
        $abc = $info['abc.zip'];
        $abc['DownloadCount']++;
        $info['abc.zip'] = $abc;
        $fileinfo->save($info);

        $fileinfo = new InfoFile($this->filename);
        $this->assertEqual("test\ntest again\n", $info['__DIR__']['Description']);
        $this->assertEqual('10001', $info['abc.zip']['DownloadCount']);
    }

}

?>
